<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
<title>Ron Paul Donation Info</title>
<script type="text/javascript">
//Updates your Y! Messenger status message with Ron Paul's donation amount for the day	
//Author: Pramod Biligiri (http://pramodb.net, pramodbiligiri@gmail.com)

function onLoad() {

    //the url from where donation info is fetched
    var INFO_URL = "http://www.ashlux.com/rp-donations/getdata.php";

    //return the status message string given the amount as $xxxx
    function getFormattedMessage(amount){
	return amount+" raised today by Ron Paul http://www.ronpaul2008.com";
    }

    // the window.external object gives you access to the Yahoo! Messenger Plugin SDK
    // I like to name it something short, like y, for easy access
    y = window.external;

    // calls a function which i've named onHttpRequestCompleted whenever event HTTPRequestCompleted is fired
    y.SetEventHandler("HTTPRequestCompleted", OnHttpRequestCompleted);

    // according to the SDK documentation (yes, you have to read it sometimes) the HTTPRequestCompleted
    // event returns two event parameters. The id is the http request id and the textstring is the textual
    // response produced by the http request. A properly written event handler function should pass these
    // returned event parameters as arguments so the function can appropriately respond to what's in them
    function OnHttpRequestCompleted(id, textstring) {
	var lines = textstring.split("\n");
	var result = null;
	for(var i=0;i<lines.length;i++){
	    var vals = lines[i].split("=");
	    if(vals[0] == "DDT"){
	        result = vals[1];
	    }
	}
	if(result != null){
	    updateStatusMessage(getFormattedMessage(result));
	}
    }

    function updateStatusMessage(message){
	y.Messenger.Me.Status.SetCustomStatus(message,true,null,null);
    }


    // calls a function which i've named onHttpRequestError whenever event HTTPRequestError is fired
    y.SetEventHandler("HTTPRequestError", onHttpRequestError);

    // event returns http request id and the type of error
    function onHttpRequestError(id, errortype) {
        y.ToastShow("httpRequestError", 5, "Error getting Ron Paul donation info: errortype:"+errortype);
    }

    // calls a function which i've named onFriendSelected whenever event FriendSelected is fired
    // according to the docs, the FriendSelected event requires an object reference to y.Messenger.Mainwindow
    // to avoid garbage collection eating your event, make a permanent reference to it in an instance var
    var mainWindow = y.Messenger.MainWindow;
    mainWindow.SetEventHandler("FriendSelected", onFriendSelected);

    // function to handle the FriendSelected event
    function onFriendSelected(friend) {
        //sendHttpRequest(INFO_URL);
    }

    // this tells Yahoo! Messenger that the plugin is ready to begin handling the events for which I've defined handlers for
    y.LocalReady();
    sendHttpRequest(INFO_URL);
    setInterval(function(){sendHttpRequest(INFO_URL);}, 300000);
}

// a function to send an http request to wherever the url goes
function sendHttpRequest(url) {
    y.SendHTTPRequest("myRequest", url);
}

</script>
</head>
<body onload="onLoad();">
Keeps updating status message with current day's fundraising of <a href="http://ronpaul2008.com">Ron Paul</a>.<br/>
(<a href="http://ronpaul-yahoo-status.googlecode.com/">Plugin homepage</a>, by <a href="http://pramodb.net">Pramod Biligiri</a>, pramodbiligiri@gmail.com)
</body>
</html>
